/*
 * @Author: worldzb 
 * @Date: 2018-10-02 11:28:31 
 * @Last Modified by: worldzb
 * @Last Modified time: 2019-01-14 15:13:05
 */

import Table from '../models/Table.class.js';
import TableSettings from '../models/TableSettings.class';


const getters={
    /**
     * 获取所有表格
     * @param {*} state 
     */
    getTables(state){
        return state.tables;
    },

    /**
     * 获取当前活动的表格 Hs
     * @param {*} state 
     * @returns 包含有HandsonTable对象
     */
    getCurrentActiveTable(state){
        if(state.currentTable){
            return state.currentTable;
        }else{
            return false;
        }
    }, 

    getLoading(state){
        return state.loading;
    }

}

const mutations={

    /**
     * 设置加载
     * @param {*} state 
     * @param {*} param1 
     */
    setLoading(state,{status,msg}){
        state.loading={
            status:status,
            msg:msg
        }
    },

    /**
     * 新建表格
     * @param {*} state 
     * @param {Object} obj 心表格初始化配置参数
     */
    newTable(state,obj){
        for(let i in state.tables){
            state.tables[i].active=false;
        }

        let table=new Table({
            title:`sheet${state.tables.length}`,
            data:TableSettings.createData(obj.row,obj.col,obj.isFull)
        })
        state.tables.push(
            table
        );
    },

    /**
     * 从外部加载数据
     * @param {Object} state 
     * @param {Object Table} table 
     */
    loadingFile(state,tab){
        //打开前先检测该文件是否被打开
        for(let i in state.tables){
            if(state.tables[i].id == tab.stmt_hash){
                state.loading={
                    status:false,
                    msg:''
                };
                for(let j in state.tables){
                    state.tables[j].active=false;
                }
                state.tables[i].active=true;
                return;
            }
        }
       
        for(let i in state.tables){
            state.tables[i].active=false;
        }
        let tableData=eval( '('+tab.stmt_content+')');

        
        let table=new Table({
            id:tab.stmt_hash,
            title:tab.stmt_title,
            data:tableData.sourceData,
            mergeCells:tableData.mergeCells,
            sourceURL:tableData.sourceURL,
            preHashTable:tableData.preHashTable,

            colWidths:tableData.colWidths,
            rowHeights:tableData.rowHeights
        });


        //文件加载状态绑定
        table.loading=function(res){
            state.loading.msg=res.msg;
        }
        
        //加载完成后的回调绑定
        table.loaded=function(){
            state.tables.push(
                table
            );

            //状态显示
            state.loading={
                status:false,
                msg:''
            }
        }
        table.loadSource();
    },

    /**
     * 切换活动的表格
     * @param {*} state 
     * @param {Number} index 
     */
    switchTable(state,index){
        for(let i in state.tables){
            state.tables[i].active=false;
        }
        state.tables[index].active=true;
    },

    //删除表格
    deleteTable(state,index){
        state.tables.splice(index,1); 
        for(let i in state.tables){
            state.tables[i].active=false;
        }
        if(state.tables.length>=2){
            if(index==0){
                state.tables[0].active=true;
            }else if(index==state.tables.length){
                state.tables[index-1].active=true;
            }else{
                state.tables[index].active=true;
            }
        }else if(state.tables.length==1){
            state.tables[0].active=true;
        }
    },

    //更新表格数据
    updateTableData(state,{index,data}){
        state.tables[index].data=data;
        console.log(data);
    },

    //更新合并数据
    updateTableMergeCells(state,{index,data}){
        state.tables[index].mergeCells=data;
    },

    //更新设置
    updateSettings(state,{index,data}){
        state.tables[index].settings=data;
    },
    
    //公式更新
    updateFormulas(state,{index,data}){
        //添加url
        let isRepeat=false;
        for(let i in state.tables[index].sourceURL){
            //如果已经有地址重复了，就不再添加
            if(state.tables[index].sourceURL[i] == data.url ){

                for(let j in data.hashTable){
                    let arrTempIndex=[];
                    arrTempIndex=arrTempIndex.concat(data.hashTable[j].index);
                    arrTempIndex.splice(0,0,i);
                    
                    let repeat=false;
                    for(let k in state.tables[index].preHashTable){
                        if(state.tables[index].preHashTable[k] == data.hashTable[j].name){
                            state.tables[index].preHashTable[k].value=arrTempIndex;
                            break;
                        }
                    }
                    if(!repeat){
                        state.tables[index].preHashTable.push({
                            key:data.hashTable[j].name,
                            value:arrTempIndex
                        });
                    }
                }
                isRepeat=true;
                break;
            }
        }
        
        if(!isRepeat){
            state.tables[index].sourceURL.push(data.url);
            for(let j in data.hashTable){
                let arrTempIndex=[];
                arrTempIndex=arrTempIndex.concat(data.hashTable[j].index);
                arrTempIndex.splice(0,0,state.tables[index].sourceURL.length-1);
                
                let repeat=false;
                for(let k in state.tables[index].preHashTable){
                    if(state.tables[index].preHashTable[k] == data.hashTable[j].name){
                        state.tables[index].preHashTable[k].value=arrTempIndex;
                        break;
                    }
                }
                if(!repeat){
                    state.tables[index].preHashTable.push({
                        key:data.hashTable[j].name,
                        value:arrTempIndex
                    });
                }
                
            }
        }
        console.log(state.tables[index].sourceURL,state.tables[index].preHashTable);
        //console.log(data.hashTable[0]);
        //加载资源
        
        state.tables[index].loadSource();
    },

    //更新单元格大小
    updateSize(state,{index,data}){
        state.tables[index][data.type][data.index]=data.value;
    },
    
    //设置当前表格
    setCurrentTable(state,ct){
        state.currentTable=ct;
    },
    //更新url地址
    updateUrl(state,{tableIndex,urlIndex,data}){

        //console.log(state.tables[tableIndex])
        state.tables[tableIndex].sourceURL[urlIndex]=data;
    },

    //删除
    deleteFormulas(state,{tableIndex,formulasIndex}){
        state.tables[tableIndex].preHashTable.splice(formulasIndex,1);
        state.tables[tableIndex].loadSource();
    }
    
    
}   


export default {
    state:{
        tables:[],//表格列表
        currentTable:'',
        loading:{
            status:false,
            msg:"正在加载"
        }
    },
    getters,
    mutations,
}